/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.datamanager;

import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;
import org.jdom.Element;
import ru.ssau.entities.*;
import ru.ssau.entities.ThreadLocal;
import ru.ssau.utils.XMLHelper;
import ru.ssau.utils.EntityUtil;
import ru.ssau.utils.JMSUtil;


public class DataManagerBean implements SessionBean {
    public static final String VALUES_XML_TAG = "values";
    //private ForumDAOFactory daoFactory;
    //private ForumDataAccessObject forumDAO;
    private SessionContext context;

    private MessageLocalHome messageHome;
    private UserLocalHome userHome;
    private ThreadLocalHome threadHome;
    private AuthorizationInfoLocalHome authInfoHome;

    public DataManagerBean() {
        
    }

    /*Thread section*/
    public void addThread(String caption) throws DataAccessException {
        try {
            ThreadLocal thread = threadHome.create(caption);
            JMSUtil.sendLogMessage("Thread", thread.getId().intValue(), new Date(), "Add");
        } catch (CreateException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex.getMessage(), ex);
        }
    }

    public void replaceThread(Integer id, String caption) throws DataAccessException {
        try {
            ThreadLocal thread = threadHome.findByPrimaryKey(id);
            thread.setCaption(caption);
            JMSUtil.sendLogMessage("Thread", thread.getId().intValue(), new Date(), "Edit");
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String getThreadXMLById(Integer id) throws DataAccessException {
        try {
            Element threadElement = threadHome.findByPrimaryKey(id).toXMLElement();
            return XMLHelper.outputXML(threadElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void deleteThreadById(Integer id) throws DataAccessException {
        try {
            threadHome.remove(id);
            JMSUtil.sendLogMessage("Thread", id.intValue(), new Date(), "Delete");
        } catch (RemoveException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String getThreadsXML() throws DataAccessException {
        try {
            Element threadsElement = new Element("threadList");
            XMLHelper.addElement(threadsElement, VALUES_XML_TAG, threadHome.getAllAsStringIntegerPairs());
            return XMLHelper.outputXML(threadsElement);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String findThreadsXMLByCaption(String caption) throws DataAccessException {
        try {
            Element threadsElement = new Element("Threads");
            XMLHelper.addElement(threadsElement, VALUES_XML_TAG, threadHome.findByCaption(caption));
            return XMLHelper.outputXML(threadsElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    /*Message section*/
    public void addMessage(String caption, String text, Integer userId, 
            Integer threadId, Date postDate) throws DataAccessException {
        try {
            MessageLocal message = messageHome.create(caption, text, userId, threadId, postDate);
            JMSUtil.sendLogMessage("Message", message.getId().intValue(), new Date(), "Add");
        } catch (CreateException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void replaceMessage(Integer id, String caption, String text, 
            Integer userId, Integer threadId, Date postDate) throws DataAccessException {
        try {
            MessageLocal message = messageHome.findByPrimaryKey(id);
            message.setCaption(caption);
            message.setText(text);
            message.setUserId(userId);
            message.setThreadId(threadId);
            message.setPostDate(postDate);

            JMSUtil.sendLogMessage("Message", message.getId().intValue(), new Date(), "Edit");
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String getMessageXMLById(Integer id) throws DataAccessException {
        try {
            Element messageElement = messageHome.findByPrimaryKey(id).toXMLElement();
            return XMLHelper.outputXML(messageElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void deleteMessageById(Integer id) throws DataAccessException {
        try {
            messageHome.remove(id);
            JMSUtil.sendLogMessage("Message", id.intValue(), new Date(), "Delete");
        } catch (RemoveException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }
    
    public String getMessagesXML() throws DataAccessException {
        try {
            Element messagesElement = new Element("messaleList");
            XMLHelper.addElement(messagesElement, VALUES_XML_TAG, messageHome.getAllAsStringIntegerPairs());
            return XMLHelper.outputXML(messagesElement);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String findMessagesXMLByCaption(String caption) throws DataAccessException {
        try {
            Element messagesElement = new Element("Messages");
            XMLHelper.addElement(messagesElement, VALUES_XML_TAG, messageHome.findByCaption(caption));
            return XMLHelper.outputXML(messagesElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    /*User section*/
    public void addUser(String name, Date regDate, String info, String login, 
            String password) throws DataAccessException {
        try {
            UserLocal user = userHome.create(name, regDate, info);
            AuthorizationInfoLocal authInfo = authInfoHome.create(user.getId(), login, password);
            JMSUtil.sendLogMessage("User", user.getId().intValue(), new Date(), "Add");
            //user.setAuthorizationInfo(authInfo);
            //user.setAuthorizationInfo(authInfo);
        } catch (CreateException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void replaceUser(Integer id, String name, Date regDate, String info, 
            String login, String password) throws DataAccessException {
        try {
            UserLocal user = userHome.findByPrimaryKey(id);
            user.setName(name);
            user.setRegistrationDate(regDate);
            user.setInfo(info);
            user.getAuthorizationInfo().setLogin(login);
            user.getAuthorizationInfo().setPassword(password);
            JMSUtil.sendLogMessage("User", user.getId().intValue(), new Date(), "Edit");
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String getUserXMLById(Integer id) throws DataAccessException {
        try {
            Element userElement = userHome.findByPrimaryKey(id).toXMLElement();
            return XMLHelper.outputXML(userElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void deleteUserById(Integer id) throws DataAccessException {
        try {
            userHome.remove(id);
            JMSUtil.sendLogMessage("User", id.intValue(), new Date(), "Delete");
        } catch (RemoveException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String getUsersXML() throws DataAccessException {
        try {
            Element usersElement = new Element("userList");
            XMLHelper.addElement(usersElement, VALUES_XML_TAG, userHome.getAllAsStringIntegerPairs());
            return XMLHelper.outputXML(usersElement);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public String findUsersXMLByName(String name) throws DataAccessException {
        try {
            Element usersElement = new Element("Users");
            XMLHelper.addElement(usersElement, VALUES_XML_TAG, userHome.findByName(name));
            return XMLHelper.outputXML(usersElement);
        } catch (FinderException ex) {
            throw new DataAccessException(ex);
        } catch (EJBException ex) {
            throw new DataAccessException(ex);
        }
    }

    public void ejbCreate() {
        try {
            userHome = EntityUtil.getUserLocalHome();
            messageHome = EntityUtil.getMessageLocalHome();
            threadHome = EntityUtil.getThreadLocalHome();
            authInfoHome = EntityUtil.getAuthorizationInfoLocalHome();
        } catch(NamingException ex) {

        }
    }

    public void setSessionContext(SessionContext ctx) {
        context = ctx;
    }

    public void ejbActivate() {

    }

    public void ejbPassivate() {

    }

    public void ejbRemove() {
        
    }

}
